//
//  WNNetworkRequest.m
//  iOSOCProjectDirectory
//
//  Created by BHmac on 2017/6/9.
//  Copyright © 2017年 XieXuan. All rights reserved.
//

#import "WNNetworkRequest.h"

@implementation WNNetworkRequest

#pragma mark - GET请求无缓存

+ (__kindof NSString *)urlString:(NSString *)url{
    return [Network_URL stringByAppendingString:url];
}

+ (NSURLSessionTask *)GET:(NSString *)URL
               parameters:(NSDictionary *)parameters
                  success:(HttpRequestSuccess)success
                  failure:(HttpRequestFailed)failure
{
    AFHTTPSessionManager *manager = [self createAFHTTPSessionManager];
    return [manager GET:[self urlString:URL] parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success ? success(responseObject) : nil;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure ? failure(error) : nil;
        
        [self showErrorInfo:error];
    }];
}

#pragma mark - POST请求无缓存

+ (__kindof NSURLSessionTask *)POST:(NSString *)URL
                         parameters:(NSDictionary *)parameters
                            success:(HttpRequestSuccess)success
                            failure:(HttpRequestFailed)failure{
    AFHTTPSessionManager *manager = [self createAFHTTPSessionManager];
    return [manager POST:[self urlString:URL] parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        success ? success(responseObject) : nil;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure ? failure(error) : nil;
        [self showErrorInfo:error];
    }];
    
    
}

#pragma mark - 网络错误
+(void)showErrorInfo:(NSError *)error{
    [MBProgressHUD hideHUD];
    if([error.localizedDescription isEqualToString:@"The request timed out."]){
        [MBProgressHUD showError:@"网络请求超时"];
    }else if ([error.localizedDescription isEqualToString:@"The Internet connection appears to be offline."]){
        [MBProgressHUD showError:@"网络链接失败，请检查网络"];
    }else if ([error.localizedDescription isEqualToString:@"Could not connect to the server."]){
        [MBProgressHUD showError:@"无法链接服务器"];
    }else{
        [MBProgressHUD showError:error.localizedDescription];
    }
    DLog(@"%@",error.localizedDescription);
    
    return;
}


#pragma mark - 设置AFHTTPSessionManager相关属性
+ (AFHTTPSessionManager *)createAFHTTPSessionManager
{
    //打开状态栏的等待菊花
    //    [AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置请求参数的类型:HTTP (AFJSONRequestSerializer,AFHTTPRequestSerializer)
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    //设置请求的超时时间
    manager.requestSerializer.timeoutInterval = 20.f;
    //设置服务器返回结果的类型:JSON (AFJSONResponseSerializer,AFHTTPResponseSerializer)
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"application/json",
                                                                              @"text/html",
                                                                              @"text/json",
                                                                              @"text/plain",
                                                                              @"text/javascript",
                                                                              @"text/xml",
                                                                              @"image/*"]];
    return manager;
}


@end
